{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('csi300lab.pkl','rb') as f:\n",
    "    lab=pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab=pickle.load(open('csi300lab.pkl','rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datetime</th>\n",
       "      <th>instrument</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2018-12-03</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <td>2.080000e-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.SZ</th>\n",
       "      <td>-2.359527e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000063.SZ</th>\n",
       "      <td>-7.960707e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000069.SZ</th>\n",
       "      <td>-1.144739e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000100.SZ</th>\n",
       "      <td>2.290000e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2022-12-30</th>\n",
       "      <th>603833.SH</th>\n",
       "      <td>1.141826e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603882.SH</th>\n",
       "      <td>3.019640e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603899.SH</th>\n",
       "      <td>5.324255e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603986.SH</th>\n",
       "      <td>2.977887e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603993.SH</th>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252960 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                              label\n",
       "datetime   instrument              \n",
       "2018-12-03 000001.SZ   2.080000e-17\n",
       "           000002.SZ  -2.359527e-02\n",
       "           000063.SZ  -7.960707e-03\n",
       "           000069.SZ  -1.144739e-02\n",
       "           000100.SZ   2.290000e-16\n",
       "...                             ...\n",
       "2022-12-30 603833.SH   1.141826e-02\n",
       "           603882.SH   3.019640e-02\n",
       "           603899.SH   5.324255e-02\n",
       "           603986.SH   2.977887e-02\n",
       "           603993.SH   0.000000e+00\n",
       "\n",
       "[252960 rows x 1 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea=fea-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>datetime</th>\n",
       "      <th>instrument</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2018-12-03</th>\n",
       "      <th>AAL</th>\n",
       "      <td>-0.074653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>-0.043989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ADBE</th>\n",
       "      <td>-0.036982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ADI</th>\n",
       "      <td>-0.032831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ADP</th>\n",
       "      <td>-0.025778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2022-12-30</th>\n",
       "      <th>WBA</th>\n",
       "      <td>3.491435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WDAY</th>\n",
       "      <td>-0.722883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WDC</th>\n",
       "      <td>2.796831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WYNN</th>\n",
       "      <td>-0.358070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>XEL</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>92364 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                          label\n",
       "datetime   instrument          \n",
       "2018-12-03 AAL        -0.074653\n",
       "           AAPL       -0.043989\n",
       "           ADBE       -0.036982\n",
       "           ADI        -0.032831\n",
       "           ADP        -0.025778\n",
       "...                         ...\n",
       "2022-12-30 WBA         3.491435\n",
       "           WDAY       -0.722883\n",
       "           WDC         2.796831\n",
       "           WYNN       -0.358070\n",
       "           XEL         0.000000\n",
       "\n",
       "[92364 rows x 1 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.reset_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea['datetime']=fea['datetime'].astype('datetime64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.to_pickle('nas90lab.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea['label']=lab['label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.rename(columns={'datetime':'dt','instrument':'kdcode'},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.to_csv('/home/sunhaoyu/thgnn/csi300.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.reset_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>instrument</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>2.080000e-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>-2.359527e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000063.SZ</td>\n",
       "      <td>-7.960707e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000069.SZ</td>\n",
       "      <td>-1.144739e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000100.SZ</td>\n",
       "      <td>2.290000e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252955</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603833.SH</td>\n",
       "      <td>1.141826e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252956</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603882.SH</td>\n",
       "      <td>3.019640e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252957</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603899.SH</td>\n",
       "      <td>5.324255e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252958</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603986.SH</td>\n",
       "      <td>2.977887e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252959</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252960 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         datetime instrument         label\n",
       "0      2018-12-03  000001.SZ  2.080000e-17\n",
       "1      2018-12-03  000002.SZ -2.359527e-02\n",
       "2      2018-12-03  000063.SZ -7.960707e-03\n",
       "3      2018-12-03  000069.SZ -1.144739e-02\n",
       "4      2018-12-03  000100.SZ  2.290000e-16\n",
       "...           ...        ...           ...\n",
       "252955 2022-12-30  603833.SH  1.141826e-02\n",
       "252956 2022-12-30  603882.SH  3.019640e-02\n",
       "252957 2022-12-30  603899.SH  5.324255e-02\n",
       "252958 2022-12-30  603986.SH  2.977887e-02\n",
       "252959 2022-12-30  603993.SH  0.000000e+00\n",
       "\n",
       "[252960 rows x 3 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>kdcode</th>\n",
       "      <th>dt</th>\n",
       "      <th>macd</th>\n",
       "      <th>boll_ub</th>\n",
       "      <th>boll_lb</th>\n",
       "      <th>rsi_30</th>\n",
       "      <th>cci_30</th>\n",
       "      <th>dx_30</th>\n",
       "      <th>close_30_sma</th>\n",
       "      <th>close_60_sma</th>\n",
       "      <th>prev_adjfactor</th>\n",
       "      <th>open_r</th>\n",
       "      <th>high_r</th>\n",
       "      <th>low_r</th>\n",
       "      <th>close_r</th>\n",
       "      <th>turnover_r</th>\n",
       "      <th>volume_r</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>570</th>\n",
       "      <td>600010.SH</td>\n",
       "      <td>2008-01-30</td>\n",
       "      <td>0.404914</td>\n",
       "      <td>0.015821</td>\n",
       "      <td>0.048071</td>\n",
       "      <td>0.391986</td>\n",
       "      <td>0.451775</td>\n",
       "      <td>0.231477</td>\n",
       "      <td>0.015778</td>\n",
       "      <td>0.016345</td>\n",
       "      <td>2.840490</td>\n",
       "      <td>0.610245</td>\n",
       "      <td>0.672703</td>\n",
       "      <td>0.498516</td>\n",
       "      <td>0.524153</td>\n",
       "      <td>0.331696</td>\n",
       "      <td>0.585292</td>\n",
       "      <td>-0.145759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>571</th>\n",
       "      <td>600028.SH</td>\n",
       "      <td>2008-01-30</td>\n",
       "      <td>0.365328</td>\n",
       "      <td>0.049023</td>\n",
       "      <td>0.069958</td>\n",
       "      <td>0.213710</td>\n",
       "      <td>0.441069</td>\n",
       "      <td>0.687134</td>\n",
       "      <td>0.046017</td>\n",
       "      <td>0.047738</td>\n",
       "      <td>1.469280</td>\n",
       "      <td>0.580691</td>\n",
       "      <td>0.664803</td>\n",
       "      <td>0.415703</td>\n",
       "      <td>0.485006</td>\n",
       "      <td>0.549299</td>\n",
       "      <td>0.591715</td>\n",
       "      <td>-0.286796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>572</th>\n",
       "      <td>600030.SH</td>\n",
       "      <td>2008-01-30</td>\n",
       "      <td>0.223751</td>\n",
       "      <td>0.197778</td>\n",
       "      <td>0.182525</td>\n",
       "      <td>0.278769</td>\n",
       "      <td>0.429452</td>\n",
       "      <td>0.517686</td>\n",
       "      <td>0.187563</td>\n",
       "      <td>0.194687</td>\n",
       "      <td>1.397980</td>\n",
       "      <td>0.602083</td>\n",
       "      <td>0.581578</td>\n",
       "      <td>0.237411</td>\n",
       "      <td>0.354074</td>\n",
       "      <td>0.615399</td>\n",
       "      <td>0.578255</td>\n",
       "      <td>-0.198683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>573</th>\n",
       "      <td>600031.SH</td>\n",
       "      <td>2008-01-30</td>\n",
       "      <td>0.403245</td>\n",
       "      <td>0.115007</td>\n",
       "      <td>0.151376</td>\n",
       "      <td>0.496849</td>\n",
       "      <td>0.506120</td>\n",
       "      <td>0.063270</td>\n",
       "      <td>0.123613</td>\n",
       "      <td>0.128296</td>\n",
       "      <td>5.782510</td>\n",
       "      <td>0.543383</td>\n",
       "      <td>0.659831</td>\n",
       "      <td>0.554059</td>\n",
       "      <td>0.543239</td>\n",
       "      <td>0.403830</td>\n",
       "      <td>0.608479</td>\n",
       "      <td>-0.083406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>574</th>\n",
       "      <td>600036.SH</td>\n",
       "      <td>2008-01-30</td>\n",
       "      <td>0.369391</td>\n",
       "      <td>0.082511</td>\n",
       "      <td>0.110925</td>\n",
       "      <td>0.338638</td>\n",
       "      <td>0.415037</td>\n",
       "      <td>0.472757</td>\n",
       "      <td>0.085105</td>\n",
       "      <td>0.088318</td>\n",
       "      <td>2.423500</td>\n",
       "      <td>0.597344</td>\n",
       "      <td>0.553847</td>\n",
       "      <td>0.008876</td>\n",
       "      <td>0.208409</td>\n",
       "      <td>0.634962</td>\n",
       "      <td>0.665964</td>\n",
       "      <td>-0.142405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102085</th>\n",
       "      <td>601398.SH</td>\n",
       "      <td>2021-12-29</td>\n",
       "      <td>0.416141</td>\n",
       "      <td>0.006864</td>\n",
       "      <td>0.042967</td>\n",
       "      <td>0.500413</td>\n",
       "      <td>0.546382</td>\n",
       "      <td>0.320748</td>\n",
       "      <td>0.008047</td>\n",
       "      <td>0.008384</td>\n",
       "      <td>1.977063</td>\n",
       "      <td>0.495026</td>\n",
       "      <td>0.462267</td>\n",
       "      <td>0.483788</td>\n",
       "      <td>0.455561</td>\n",
       "      <td>0.434999</td>\n",
       "      <td>0.508664</td>\n",
       "      <td>0.006508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102086</th>\n",
       "      <td>601601.SH</td>\n",
       "      <td>2021-12-29</td>\n",
       "      <td>0.406976</td>\n",
       "      <td>0.052239</td>\n",
       "      <td>0.095923</td>\n",
       "      <td>0.449307</td>\n",
       "      <td>0.450983</td>\n",
       "      <td>0.005018</td>\n",
       "      <td>0.060156</td>\n",
       "      <td>0.063654</td>\n",
       "      <td>1.392600</td>\n",
       "      <td>0.539150</td>\n",
       "      <td>0.499614</td>\n",
       "      <td>0.498843</td>\n",
       "      <td>0.456567</td>\n",
       "      <td>0.453417</td>\n",
       "      <td>0.539629</td>\n",
       "      <td>-0.004047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102087</th>\n",
       "      <td>601628.SH</td>\n",
       "      <td>2021-12-29</td>\n",
       "      <td>0.422471</td>\n",
       "      <td>0.056954</td>\n",
       "      <td>0.101903</td>\n",
       "      <td>0.515735</td>\n",
       "      <td>0.549422</td>\n",
       "      <td>0.275174</td>\n",
       "      <td>0.065164</td>\n",
       "      <td>0.068478</td>\n",
       "      <td>1.251853</td>\n",
       "      <td>0.562199</td>\n",
       "      <td>0.514177</td>\n",
       "      <td>0.495189</td>\n",
       "      <td>0.464361</td>\n",
       "      <td>0.326423</td>\n",
       "      <td>0.534538</td>\n",
       "      <td>0.035334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102088</th>\n",
       "      <td>601857.SH</td>\n",
       "      <td>2021-12-29</td>\n",
       "      <td>0.415025</td>\n",
       "      <td>0.007579</td>\n",
       "      <td>0.043485</td>\n",
       "      <td>0.468768</td>\n",
       "      <td>0.510243</td>\n",
       "      <td>0.062119</td>\n",
       "      <td>0.008645</td>\n",
       "      <td>0.009622</td>\n",
       "      <td>1.452037</td>\n",
       "      <td>0.515893</td>\n",
       "      <td>0.480839</td>\n",
       "      <td>0.496039</td>\n",
       "      <td>0.456530</td>\n",
       "      <td>0.411989</td>\n",
       "      <td>0.533228</td>\n",
       "      <td>0.002045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102089</th>\n",
       "      <td>601919.SH</td>\n",
       "      <td>2021-12-29</td>\n",
       "      <td>0.444228</td>\n",
       "      <td>0.035866</td>\n",
       "      <td>0.073797</td>\n",
       "      <td>0.560723</td>\n",
       "      <td>0.547770</td>\n",
       "      <td>0.283644</td>\n",
       "      <td>0.037615</td>\n",
       "      <td>0.037080</td>\n",
       "      <td>1.355744</td>\n",
       "      <td>0.528307</td>\n",
       "      <td>0.557419</td>\n",
       "      <td>0.470430</td>\n",
       "      <td>0.440412</td>\n",
       "      <td>0.661572</td>\n",
       "      <td>0.532738</td>\n",
       "      <td>0.066743</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>101520 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           kdcode          dt      macd   boll_ub   boll_lb    rsi_30  \\\n",
       "570     600010.SH  2008-01-30  0.404914  0.015821  0.048071  0.391986   \n",
       "571     600028.SH  2008-01-30  0.365328  0.049023  0.069958  0.213710   \n",
       "572     600030.SH  2008-01-30  0.223751  0.197778  0.182525  0.278769   \n",
       "573     600031.SH  2008-01-30  0.403245  0.115007  0.151376  0.496849   \n",
       "574     600036.SH  2008-01-30  0.369391  0.082511  0.110925  0.338638   \n",
       "...           ...         ...       ...       ...       ...       ...   \n",
       "102085  601398.SH  2021-12-29  0.416141  0.006864  0.042967  0.500413   \n",
       "102086  601601.SH  2021-12-29  0.406976  0.052239  0.095923  0.449307   \n",
       "102087  601628.SH  2021-12-29  0.422471  0.056954  0.101903  0.515735   \n",
       "102088  601857.SH  2021-12-29  0.415025  0.007579  0.043485  0.468768   \n",
       "102089  601919.SH  2021-12-29  0.444228  0.035866  0.073797  0.560723   \n",
       "\n",
       "          cci_30     dx_30  close_30_sma  close_60_sma  prev_adjfactor  \\\n",
       "570     0.451775  0.231477      0.015778      0.016345        2.840490   \n",
       "571     0.441069  0.687134      0.046017      0.047738        1.469280   \n",
       "572     0.429452  0.517686      0.187563      0.194687        1.397980   \n",
       "573     0.506120  0.063270      0.123613      0.128296        5.782510   \n",
       "574     0.415037  0.472757      0.085105      0.088318        2.423500   \n",
       "...          ...       ...           ...           ...             ...   \n",
       "102085  0.546382  0.320748      0.008047      0.008384        1.977063   \n",
       "102086  0.450983  0.005018      0.060156      0.063654        1.392600   \n",
       "102087  0.549422  0.275174      0.065164      0.068478        1.251853   \n",
       "102088  0.510243  0.062119      0.008645      0.009622        1.452037   \n",
       "102089  0.547770  0.283644      0.037615      0.037080        1.355744   \n",
       "\n",
       "          open_r    high_r     low_r   close_r  turnover_r  volume_r     label  \n",
       "570     0.610245  0.672703  0.498516  0.524153    0.331696  0.585292 -0.145759  \n",
       "571     0.580691  0.664803  0.415703  0.485006    0.549299  0.591715 -0.286796  \n",
       "572     0.602083  0.581578  0.237411  0.354074    0.615399  0.578255 -0.198683  \n",
       "573     0.543383  0.659831  0.554059  0.543239    0.403830  0.608479 -0.083406  \n",
       "574     0.597344  0.553847  0.008876  0.208409    0.634962  0.665964 -0.142405  \n",
       "...          ...       ...       ...       ...         ...       ...       ...  \n",
       "102085  0.495026  0.462267  0.483788  0.455561    0.434999  0.508664  0.006508  \n",
       "102086  0.539150  0.499614  0.498843  0.456567    0.453417  0.539629 -0.004047  \n",
       "102087  0.562199  0.514177  0.495189  0.464361    0.326423  0.534538  0.035334  \n",
       "102088  0.515893  0.480839  0.496039  0.456530    0.411989  0.533228  0.002045  \n",
       "102089  0.528307  0.557419  0.470430  0.440412    0.661572  0.532738  0.066743  \n",
       "\n",
       "[101520 rows x 18 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.reset_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab=lab.sort_values(by=['instrument','datetime'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.reset_index(inplace=True,drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab['label']=lab['label'].shift(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab['label'].fillna(0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.sort_values(by=['datetime','instrument'],inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.set_index(['datetime','instrument'],inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>instrument</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>open</th>\n",
       "      <th>to</th>\n",
       "      <th>vol</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-12-03</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>0.018347</td>\n",
       "      <td>0.018254</td>\n",
       "      <td>0.018982</td>\n",
       "      <td>0.018458</td>\n",
       "      <td>0.125469</td>\n",
       "      <td>0.172416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>255</th>\n",
       "      <td>2018-12-04</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>0.018347</td>\n",
       "      <td>0.018178</td>\n",
       "      <td>0.019103</td>\n",
       "      <td>0.018419</td>\n",
       "      <td>0.063019</td>\n",
       "      <td>0.086656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>510</th>\n",
       "      <td>2018-12-05</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>0.018077</td>\n",
       "      <td>0.017969</td>\n",
       "      <td>0.018841</td>\n",
       "      <td>0.018265</td>\n",
       "      <td>0.067741</td>\n",
       "      <td>0.094228</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>765</th>\n",
       "      <td>2018-12-06</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>0.017693</td>\n",
       "      <td>0.017702</td>\n",
       "      <td>0.018499</td>\n",
       "      <td>0.018014</td>\n",
       "      <td>0.066239</td>\n",
       "      <td>0.093794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1020</th>\n",
       "      <td>2018-12-07</td>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>0.017750</td>\n",
       "      <td>0.017740</td>\n",
       "      <td>0.018579</td>\n",
       "      <td>0.017898</td>\n",
       "      <td>0.039110</td>\n",
       "      <td>0.055125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251939</th>\n",
       "      <td>2022-12-26</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.006692</td>\n",
       "      <td>0.006643</td>\n",
       "      <td>0.006965</td>\n",
       "      <td>0.006784</td>\n",
       "      <td>0.024745</td>\n",
       "      <td>0.079512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252194</th>\n",
       "      <td>2022-12-27</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.006866</td>\n",
       "      <td>0.006776</td>\n",
       "      <td>0.007065</td>\n",
       "      <td>0.006803</td>\n",
       "      <td>0.025066</td>\n",
       "      <td>0.079302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252449</th>\n",
       "      <td>2022-12-28</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.006923</td>\n",
       "      <td>0.006910</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.006861</td>\n",
       "      <td>0.033361</td>\n",
       "      <td>0.104510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252704</th>\n",
       "      <td>2022-12-29</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.006769</td>\n",
       "      <td>0.006814</td>\n",
       "      <td>0.007085</td>\n",
       "      <td>0.006900</td>\n",
       "      <td>0.032308</td>\n",
       "      <td>0.102290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>252959</th>\n",
       "      <td>2022-12-30</td>\n",
       "      <td>603993.SH</td>\n",
       "      <td>0.006731</td>\n",
       "      <td>0.006757</td>\n",
       "      <td>0.007065</td>\n",
       "      <td>0.006861</td>\n",
       "      <td>0.022141</td>\n",
       "      <td>0.070348</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>252960 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         datetime instrument     close      high       low      open  \\\n",
       "0      2018-12-03  000001.SZ  0.018347  0.018254  0.018982  0.018458   \n",
       "255    2018-12-04  000001.SZ  0.018347  0.018178  0.019103  0.018419   \n",
       "510    2018-12-05  000001.SZ  0.018077  0.017969  0.018841  0.018265   \n",
       "765    2018-12-06  000001.SZ  0.017693  0.017702  0.018499  0.018014   \n",
       "1020   2018-12-07  000001.SZ  0.017750  0.017740  0.018579  0.017898   \n",
       "...           ...        ...       ...       ...       ...       ...   \n",
       "251939 2022-12-26  603993.SH  0.006692  0.006643  0.006965  0.006784   \n",
       "252194 2022-12-27  603993.SH  0.006866  0.006776  0.007065  0.006803   \n",
       "252449 2022-12-28  603993.SH  0.006923  0.006910  0.007085  0.006861   \n",
       "252704 2022-12-29  603993.SH  0.006769  0.006814  0.007085  0.006900   \n",
       "252959 2022-12-30  603993.SH  0.006731  0.006757  0.007065  0.006861   \n",
       "\n",
       "              to       vol  \n",
       "0       0.125469  0.172416  \n",
       "255     0.063019  0.086656  \n",
       "510     0.067741  0.094228  \n",
       "765     0.066239  0.093794  \n",
       "1020    0.039110  0.055125  \n",
       "...          ...       ...  \n",
       "251939  0.024745  0.079512  \n",
       "252194  0.025066  0.079302  \n",
       "252449  0.033361  0.104510  \n",
       "252704  0.032308  0.102290  \n",
       "252959  0.022141  0.070348  \n",
       "\n",
       "[252960 rows x 8 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fea.sort_values(by=['instrument','datetime'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab['datetime']=lab['datetime'].astype('datetime64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.set_index(['datetime','instrument'],inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "lab.to_pickle('csi300lab.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "fea.drop(columns='news',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.16 ('qlib': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "87496c78f2ce0f880180f5f21e88f5b0eed8306ea5075eae3f7b1cde61f671d8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
